#pragma once


#include<assert.h>
#include<iostream>

using namespace std;

namespace yyh
{
	template<class T>
	struct list_node
	{
		list_node<T>* _next;
		list_node<T>* _prev;
		T _data;

		list_node(const T& x = T())
			:_next(nullptr)
			, _prev(nullptr)
			, _data(x)
		{ }
	};

	template<class T, class Ref, class Ptr>
	struct list_iterator
	{
		typedef list_node<T> Node;
		typedef list_iterator<T, Ref, Ptr> Self;

		Node* _node;

		list_iterator(Node* node)
			:_node(node)
		{ }

		Ref operator *()
		{
			return _node->_data;
		}

		Ptr operator ->()
		{
			return &_node->_data;
		}

		Self& operator ++()
		{
			_node = _node->_next;
			return *this;
		}

		Self operator ++(int)
		{
			Self temp(*this);
			_node = _node->_next;
			return temp;
		}

		Self& operator --()
		{
			_node = _node->_prev;
			return *this;
		}

		Self operator --(int)
		{
			Self temp(*this);
			_node = _node->_prev;
			return temp;
		}

		bool operator !=(const Self& it)
		{
			return _node != it._node;
		}

		bool operator ==(const Self& it)
		{
			return _node == it._node;
		}
	};

	template<class T>
	class list
	{
		typedef list_node<T> Node;
	public:
		typedef list_iterator<T, T&, T*> iterator;
		typedef list_iterator<T, const T&, const T*> const_iterator;

		iterator begin()
		{
			return iterator(_head->_next);
		}

		iterator end()
		{
			return iterator(_head);
		}

		const_iterator begin() const
		{
			return const_iterator(_head->_next);
		}

		const_iterator end() const
		{
			return const_iterator(_head);
		}

		void empty_init()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
			_size = 0;
		}

		list()
		{
			empty_init();
		}
		
		list(const list<T>& l)
		{
			empty_init();
			for (auto& e : l)
			{
				push_back(e);
			}
		}

		list(initializer_list<T> l)
		{
			empty_init();
			for (auto& e : l)
			{
				push_back(e);
			}
		}

		void swap(list<T>& l)
		{
			std::swap(_head, l._head);
			std::swap(_size, l._size);
		}

		list<T>& operator =(list<T> l)
		{
			swap(l);
			return *this;
		}

		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
			_size = 0;
		}

		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				it = erase(it);
			}
		}

		size_t size() const
		{
			return _size;
		}

		void push_back(const T& x)
		{
			insert(end(), x);
		}

		void push_front(const T& x)
		{
			insert(begin(), x);
		}

		void pop_back()
		{
			erase(--end());
		}

		void pop_front()
		{
			erase(begin());
		}

		void insert(iterator pos, const T& x)
		{
			Node* pcur = pos._node;
			Node* prev = pos._node->_prev;
			Node* NewNode = new Node(x);
			prev->_next = NewNode;
			NewNode->_prev = prev;
			NewNode->_next = pcur;
			pcur->_prev = NewNode;
			_size++;
		}

		iterator erase(iterator pos)
		{
			assert(pos != end());
			Node* pcur = pos._node;
			Node* pnext = pcur->_next;
			Node* prev = pcur->_prev;
			prev->_next = pnext;
			pnext->_prev = prev;
			delete pcur;
			_size--;
			return iterator(pnext);
		}

	private:
		Node* _head;
		size_t _size;
	};
}